package com.repair.worker.facade;

import com.repair.api.worker.constant.WorkerOperateType;
import com.repair.api.worker.request.*;
import com.repair.api.worker.request.condition.WorkerCondition;
import com.repair.api.worker.request.condition.WorkerIdQueryCondition;
import com.repair.api.worker.request.condition.WorkerPhoneQueryCondition;
import com.repair.api.worker.request.param.WorkerBlockParam;
import com.repair.api.worker.request.param.WorkerQueryParam;
import com.repair.api.worker.request.param.WorkerRealNameAuthParam;
import com.repair.api.worker.response.WorkerOperateResponse;
import com.repair.api.worker.response.WorkerQueryResponse;
import com.repair.api.worker.response.data.WorkerInfo;
import com.repair.api.worker.service.WorkerFacadeService;
import com.repair.base.exception.SystemErrorCode;
import com.repair.base.exception.SystemException;
import com.repair.base.response.PageResponse;
import com.repair.rpc.config.Facade;
import com.repair.worker.pojo.entity.Worker;
import com.repair.worker.service.WorkerOperateStreamService;
import com.repair.worker.service.WorkerService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * Created by IntelliJ IDEA.
 *
 * @author zq
 * @version 1.0
 * @create 2024-09-29 16:14
 * @since jdk1.8
 */
@Slf4j
@DubboService
public class WorkerFacadeServiceImpl implements WorkerFacadeService {
    @Autowired
    private WorkerService workerService;
    @Autowired
    private WorkerOperateStreamService workerOperateStreamService;

    @Override
    @Facade
    public WorkerQueryResponse<WorkerInfo> query(WorkerQueryRequest workerQueryRequest) {
        WorkerCondition workerCondition = workerQueryRequest.getWorkerCondition();
        Worker worker = null;
        if (workerCondition instanceof WorkerPhoneQueryCondition) {
            worker = workerService.selectByPhone(((WorkerPhoneQueryCondition) workerCondition).getPhone());
        } else if (workerCondition instanceof WorkerIdQueryCondition) {
            worker = workerService.getWorkerById(((WorkerIdQueryCondition) workerCondition).getId());
        }
        WorkerQueryResponse<WorkerInfo> workerInfoWorkerQueryResponse = new WorkerQueryResponse<>();
        if (worker == null) {
            log.warn("No worker found for phone: {}", ((WorkerPhoneQueryCondition) workerCondition).getPhone());
            workerInfoWorkerQueryResponse.setSuccess(false);
            return workerInfoWorkerQueryResponse;
        }

        WorkerInfo workerInfo = new WorkerInfo();
        BeanUtils.copyProperties(worker, workerInfo);
        workerInfoWorkerQueryResponse.setData(workerInfo);
        workerInfoWorkerQueryResponse.setSuccess(true);
        return workerInfoWorkerQueryResponse;
    }

    @Override
    @Facade
    public WorkerOperateResponse register(WorkerRegisterRequest workerRegisterRequest) {
        return workerService.register(workerRegisterRequest);
    }

    @Override
    @Facade
    public WorkerOperateResponse loginStream(WorkerLoginStreamRequest<WorkerInfo> workerLoginStreamRequest) {
        WorkerInfo data = workerLoginStreamRequest.getData();
        Worker worker = new Worker();
        BeanUtils.copyProperties(data, worker);
        long l = workerOperateStreamService.insertStream(worker, WorkerOperateType.LOGIN);
        WorkerOperateResponse workerOperateResponse = new WorkerOperateResponse();
        if (l == 0) {
            throw new SystemException(SystemErrorCode.INSERT_FAILED);
        }
        workerOperateResponse.setSuccess(true);
        return workerOperateResponse;
    }

    @Override
    @Facade
    public PageResponse<WorkerInfo> getAllWorkers(WorkerQueryPageRequest<WorkerQueryParam> queryParam) {
        return workerService.getAllWorkers(queryParam);
    }

    @Override
    public void updateWorkerAuth(WorkerAuthRealNameRequest<WorkerRealNameAuthParam> request) {

        workerService.updateWorkerAuth(request);
    }

    @Override
    @Facade
    public WorkerQueryResponse<Boolean> blockWorker(WorkerBlockRequest<WorkerBlockParam> param) {
        return workerService.blockWorker(param);
    }
    @Override
    @Facade
    public WorkerQueryResponse<Boolean> cancelBlockWorker(WorkerBlockRequest<WorkerBlockParam> param) {
        return workerService.cancelBlockWorker(param);
    }


}
